package com.handpay.zztong.hp;

import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.notice.ApiNoticeFactory;
import com.handpay.framework.notice.ApiNoticeListener;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.paymentbill.model.Bill;
import com.handpay.zztong.hp.paymentbill.activity.QueryBillsActivity;
import com.handpay.zztong.hp.swiper.SwiperUtils;

import java.text.DecimalFormat;
import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * 撤销结果
 * @author fplei
 *
 */
public class RescissionResultActivity extends ZZTongActivity {
	private ImageView img_revoke_state;
	private TextView txt_revoke_operation,txt_shoper_name,txt_transfer_money,txt_transfer_batch,txt_fail_reson,txt_revoking_tip,txt_transfer;
	private Button btn_back;
	private View split_line_up,split_line_down;
	private LinearLayout linear_revoke_fail;
	private Bill mBill;
	
	@Override
	protected boolean isShownTopBar() {
		return true;
	}
	
	@Override
	public ActionBar getActivityActionBar() {
		return new ActionBar(this.getString(R.string.recission_result_title), true,new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finshRescission();
			}
		});
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_revokresult);
		super.onCreate(savedInstanceState);
		
		mBill=(Bill) getIntent().getSerializableExtra(QueryBillsActivity.billvalue);
		initView();
		//微信支付宝撤销不需要连接刷卡器
		if(mBill.getTradeType()==Bill.TRAD_TYPE_POSPAY){
			if(checkVPOS(this, true)){
				doRevoke();
			}
		}else{
			doRevoke();
		}
	}

	private void initView() {
		img_revoke_state=(ImageView)findViewById(R.id.img_revoke_state);
		txt_revoke_operation=(TextView)findViewById(R.id.txt_revoke_operation);
		txt_shoper_name=(TextView)findViewById(R.id.txt_shoper_name);
		txt_shoper_name.setText(mBill.getShopName());
		txt_transfer=(TextView)findViewById(R.id.txt_transfer);
		txt_transfer_money=(TextView)findViewById(R.id.txt_transfer_money);
		double amV = (mBill.getAmount() / 100.00);
		DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
		String p = decimalFormat.format(amV);
		//这里暂时这么写当收款金额为0.01   p经过转换后变成 .01
		if(p!=null&&p.contains(".")&&p.substring(0, 1).equals(".")){
			p="0"+p;
		}
		txt_transfer_money.setText(p+getString(R.string.new_my_headSKY));
		txt_transfer_batch=(TextView)findViewById(R.id.txt_transfer_batch);
		txt_transfer_batch.setText(mBill.getTransSN());
		
		txt_fail_reson=(TextView)findViewById(R.id.txt_fail_reson);
		txt_revoking_tip=(TextView)findViewById(R.id.txt_revoking_tip);
		btn_back=(Button)findViewById(R.id.btn_back);
		btn_back.setOnClickListener(this);
		split_line_up=findViewById(R.id.split_line_up);
		split_line_down=findViewById(R.id.split_line_down);
	    linear_revoke_fail=(LinearLayout)findViewById(R.id.linear_revoke_fail);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v.getId()==R.id.btn_back){
			finshRescission();
		}
	}
	
	//撤销
	private void doRevoke(){
		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("transSN", SecureManager.getInstance().des(mBill.getTransSN(),1,null));
		params.put("transTime",mBill.getTransTime());
		params.put("amount", SecureManager.getInstance().des(String.valueOf(mBill.getAmount()),1,null));
		params.put("csn", SecureManager.getInstance().des(SwiperUtils.getCsn(),1,null));
		doPost(this, Do_zztRevokeTransaction, params);
	}
	private void finshRescission(){
		ApiNoticeFactory.getInstance().doBroadCastNotice(ApiNoticeListener.RESCISSION_ACTION,null);
		finish();
	}
	@Override
	public void goToVPos() {

		super.goToVPos();
		doRevoke();
	}
	@Override
	public boolean netResponse(String action, Hashtable<String, Object> resp,
			boolean showAlert, OnClickListener on) {
		closeProgressDialog();
		if(action.equals(Do_zztRevokeTransaction)){
			LuaTable luaTable = null;
			//撤销成功或者失败  1成功  2失败
			int status=2;
			String failMessage="";
			try {
				byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
				luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
				// 网络超时会返回Null需要判断
				if (luaTable == null) {
					/*
					 * if (super.netResponse(action, resp, showAlert, on)) { return
					 * true; }
					 */
					return true;
				}
				Object obj = luaTable.rawget("status");
				if (obj instanceof Integer) {
					status = (Integer) obj;
				} else if (obj instanceof Double) {
					double dObj = (Double) obj;
					status = (int) dObj;
				}else{
					status=Integer.parseInt(obj.toString());
				}
				//撤销失败则获取失败信息
				if(status==2){
					failMessage=(String)luaTable.rawget("message");
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			changeStateView(status,failMessage);
		}
		return super.netResponse(action, resp, showAlert, on);
	}
	//更改view的显示状态
	private void changeStateView(int code,String failmessage){
		if(code==1){
			img_revoke_state.setBackgroundResource(R.drawable.tradesuccess_icon);
			txt_revoke_operation.setText(R.string.revoke_success);
			linear_revoke_fail.setVisibility(View.GONE);//隐藏失败原因布局
			split_line_up.setVisibility(View.VISIBLE);
			split_line_down.setVisibility(View.INVISIBLE);
			txt_transfer.setText(R.string.revoke_money);//撤销功能
			txt_revoking_tip.setVisibility(View.GONE);//隐藏温馨提示
		}else{
			img_revoke_state.setBackgroundResource(R.drawable.tradefail_icon);
			txt_revoke_operation.setText(R.string.revoke_fail);
			linear_revoke_fail.setVisibility(View.VISIBLE);//隐藏失败原因布局
			txt_fail_reson.setText(failmessage);
			txt_revoking_tip.setVisibility(View.VISIBLE);//隐藏温馨提示
			split_line_up.setVisibility(View.VISIBLE);
			split_line_down.setVisibility(View.VISIBLE);
		}
		btn_back.setVisibility(View.VISIBLE);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finshRescission();
		}
		return true;
	}
}
